Skip to content

Return an error instead of panicking when Set targets a pointer to a nil map - #164

Merged
fredbi merged 1 commit into
masterfrom
copilot/fix-nil-map-pointer-panics
Nov 17, 2026
Merged

fredbi merged 1 commit into
masterfrom
copilot/fix-nil-map-pointer-panics

Conversation

@fredbi

@fredbi fredbi commented Nov 17, 2026 •

Copy link
Copy Markdown
Member

Pointer.Set and SetForToken returned an error for a nil map passed by value, but panicked with assignment to entry in nil map when given a pointer to a nil map. This happens with a zero-value map or one decoded from JSON null.

var document map[string]any
p, _ := jsonpointer.New("/key")

_, err := p.Set(document, 1)  // error
_, err = p.Set(&document, 1)  // panicked; now returns an error
  • pointer.go
    • setSingleImpl now checks whether the map is nil in its reflect.Map branch, before calling SetMapIndex.
    • The up-front isNil(node) check only sees the non-nil pointer, so it didn't catch this case.
    • The error is the same one the by-value path returns: cannot set field %q on nil value, wrapping ErrPointer.
  • pointer_test.go
    • Added TestSetPointerToNilMap. It covers Pointer.Set and SetForToken and asserts that neither panics and that the error wraps ErrPointer.

Nil maps are rejected rather than allocated, which matches the existing by-value behavior and the package's rule of not creating intermediate values.

Copilot AI linked an issue Nov 17, 2026 that may be closed by this pull request
@fredbi fredbi changed the title [WIP] Fix panics when document is a pointer to a nil map Return an error instead of panicking when Set targets a pointer to a nil map Nov 17, 2026
when passed a pointer to a nil map.

* fixes #163

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Signed-off-by: Frédéric BIDON <fredbi@yahoo.com>
@fredbi
fredbi force-pushed the copilot/fix-nil-map-pointer-panics branch from 4bd5952 to 1da7acf Compare November 17, 2026 19:43
@fredbi
fredbi marked this pull request as ready for review November 17, 2026 19:45
@fredbi
fredbi merged commit 7eaee6b into master Nov 17, 2026
20 checks passed
@fredbi
fredbi deleted the copilot/fix-nil-map-pointer-panics branch November 17, 2026 19:56
@codecov

codecov Bot commented Nov 17, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.74%. Comparing base (d8e268c) to head (1da7acf).
⚠️ Report is 5 commits behind head on master.
✅ All tests successful. No failed tests found.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #164      +/-   ##
==========================================
+ Coverage   93.72%   93.74%   +0.01%     
==========================================
  Files           5        5              
  Lines         669      671       +2     
==========================================
+ Hits          627      629       +2     
  Misses         42       42              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Set panics when the document is a pointer to a nil map

2 participants